<?php
class Blog_Block_Category extends Epic_View_Block 
{	
	/**
	 * Get all categories
	 */
	public function getCategories() 
	{
		return Epic::getModel('blog/category')->getCollection()->load();
	}
	
	/**
	 * Get all active categories
	 */
	public function getAllActiveCategories()
	{
		$enableCacheData = Epic::getRegistry()->get('config')->getConfig('cache/cache_data');
		
		// Handler cache query data
		if($enableCacheData) {
			$cache = new Epic_Cache_Handler('DATA');
			$cache->setCacheDir(CACHE_DIR . 'data')
				  ->setExpireTime(10);
			// file cache
			$fileNameCache = 'all-active-categories.txt';
			
			// create cache file if not exists or expire
			if (!$cache->exists($fileNameCache)) {
				$categoryCollection = Epic::getModel('blog/category')->getCollection()
										->field('category_id, name')
										->where('active', array('eq' => 1))
										->load();
				$cache->set($fileNameCache, $categoryCollection);
				return $categoryCollection;
			}
			return $cache->get($fileNameCache);
		} 
		
		return Epic::getModel('blog/category')->getCollection()
				->field('category_id, name')
				->where('active', array('eq' => 1))
				->load();
	}
}